//
//  VisitorViewController.swift
//  WeiBo_Private
//
//  Created by Yangperlove on 16/1/10.
//  Copyright © 2016年 Yangperlove. All rights reserved.
//

import UIKit
import SnapKit

//定义协议
@objc protocol VisitorDegate {
    //协议方法,如果要定义可选方法,需要加 @objc
    func userWillLogin()
    func userWillRegister()
}

class VisitorView: UIView {
    //创建代理属性
    weak var visitorDegate: VisitorDegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUIWithInfo(imageName: String?, tipText: String) {
        if let imgName = imageName {
            circleView.image = UIImage(named: imgName)
            descLabel.text = tipText
            
            houseView.hidden = true
            coverView.hidden = true
        }else {
            circleAnimation()
        }
    }
    
    func circleAnimation() {
        let basicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        basicAnimation.repeatCount = MAXFLOAT
        basicAnimation.duration = 10
        basicAnimation.toValue = 2 * M_PI

        //完成后不移除动画
        basicAnimation.removedOnCompletion = false
        circleView.layer.addAnimation(basicAnimation, forKey: nil)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
        setupUI()
        
    }
    
    func setupUI() {
        addSubview(circleView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(descLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
    }
    
    //设置子控件的frame
    override func layoutSubviews() {
        circleView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(snp_centerX)
            make.top.equalTo(snp_top).offset(180)
        }
        
        coverView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(snp_centerX)
            make.bottom.equalTo(descLabel.snp_top).offset(-20)
        }
        
        houseView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleView.snp_center)
        }
        
        descLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(snp_centerX)
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.width.equalTo(260)
            make.height.equalTo(60)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(descLabel.snp_bottom).offset(10)
            make.left.equalTo(descLabel.snp_left)
            make.width.equalTo(110)
            make.height.equalTo(40)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(descLabel.snp_bottom).offset(10)
            make.right.equalTo(descLabel.snp_right)
            make.width.equalTo(110)
            make.height.equalTo(40)
        }
        
        //添加监听事件
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)

    }
    
    
    
    //懒加载控件
    //圆形图片
    lazy var circleView: UIImageView = {
        let cir = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return cir
    }()
    
    //遮罩View
    lazy var coverView: UIImageView = {
        let cover = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return cover
    }()
    
    //房子图片
    lazy var houseView:UIImageView = {
       let house = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return house
    }()
    
    //文字Label
    lazy var descLabel: UILabel = UILabel(title: "登录后,别人评论你的微薄别人评论你的微薄别人评论你的微", color: UIColor.grayColor(), fontSize: 17)
   
    //登录按钮
    lazy var loginBtn: UIButton = UIButton(backImageName: "common_button_white_disable", stretchable: true, title: "登录", fontSize: 17, titleColor: UIColor.grayColor())
//    {
//        let btn = UIButton()
//        let image = UIImage(named: "common_button_white_disable")
//        let w = Int(image!.size.width * 0.5)
//        let h = Int(image!.size.height * 0.5)
//        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(w , topCapHeight: h), forState: .Normal)
//        btn.setTitle("登录", forState: .Normal)
//        btn.setTitleColor(UIColor.grayColor(), forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
//        btn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
//        
//        return btn
//    }()
    
    //注册按钮
    lazy var registerBtn: UIButton = UIButton(backImageName: "common_button_white_disable", stretchable:  true, title: "注册", fontSize: 17, titleColor: UIColor.orangeColor())
        
//        let btn = UIButton()
//        let image = UIImage(named: "common_button_white_disable")
//        let w = Int(image!.size.width * 0.5)
//        let h = Int(image!.size.height * 0.5)
//        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(w , topCapHeight: h), forState: .Normal)
//        btn.setTitle("注册", forState: .Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
//        btn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
//        
//        return btn
//    }()
    
    func loginBtnClick() {
        visitorDegate?.userWillLogin()
    }
    
    func registerBtnClick() {
        visitorDegate?.userWillRegister()
    }
    
    
    
    
    
}



